﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.IO;
using System.Drawing;
public partial class administrator_uc_admin_e_category : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]))
            {
                //LoadDataToDropDownList(DropdownParentID, 0);
                LoadDataInfo(int.Parse(Request.QueryString["itemid"].ToString().Trim()));                
            }
        }
    }
    private void LoadDataToDropDownList(DropDownList cboName, int CateID)
    {
        ListItem list = new ListItem();
        cboName.Items.Clear();
        cboName.Items.Add(list);
        list.Selected = true;
        list.Value = "0";
        list.Text = "---Chọn danh mục cha---";
        DataTable dtb = new CmsBLL().Category_GetTree(CateID);
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "_Title", "CateID");
        }
    }
    private void LoadDataInfo(int ItemID)
    {
        DataTable dt = new BLL.CmsBLL().Category_GetTree(ItemID);
        if (dt.Rows.Count > 0)
        {
            hdItemID.Value = dt.Rows[0]["CateID"] + "";
            //DropdownParentID.SelectedValue = dt.Rows[0]["ParentID"] + "";
            txtTitle.Text = dt.Rows[0]["Title"] + "";          
            txtUrl.Text = dt.Rows[0]["Url"] + "";        
            rdActive.SelectedValue = dt.Rows[0]["Active"] + "";
            LoadImage(dt.Rows[0]["Image"].ToString());
        }
        else
            lblMessage.Text = "Load data failt!";
    }
    private void LoadImage(string FileName)
    {
        try
        {
            ltViewImage.Text = "<img src=\"../smallimages/" + FileName + "\" />\n";
        }
        catch (Exception ex)
        {
            ltViewImage.Text = "Load image error! " + ex.Message;
        }
    }
    protected void lbtEditItem_Click(object sender, EventArgs e)
    {       
        if (FileUploadImage.HasFile)
        {
            string strFileName = FileUploadImage.FileName;
            try
            {
                strFileName = FileUploadImage.FileName;
                Stream fStream = FileUploadImage.FileContent;
                strFileName = FileUploadImage.FileName.Replace(' ', '_');
                strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
                strFileName = CommonBLL.GetFilename(strFileName);

                string filepathsmallimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "smallimages//") + strFileName;
                //string filepathlargeimage = Server.MapPath("~/images//") + strFileName;

                // Validate the uploaded file
                // 1. The uploaded file should not be a blank file
                if (FileUploadImage.FileBytes.Length == 0)
                {
                    lblMessage.Text = "File bạn chọn bị lỗi! dung lượng file = " + FileUploadImage.FileBytes.Length.ToString() + " bytes";
                    return;
                }
                // 2. The uploaded file should be having one of the allowed extensions
                string ext = System.IO.Path.GetExtension(FileUploadImage.FileName).TrimStart(".".ToCharArray()).ToLower();
                if ((ext != "jpeg") && (ext != "jpg") && (ext != "png") && (ext != "gif") && (ext != "bmp"))
                {
                    lblMessage.Text = "Định dạng file bạn chọn không hợp lệ (File hợp lệ dạng Ảnh)";
                    return;
                }

                // Validation successful
                // Load the image into Bitmap Object

                Bitmap uploadedImage = new Bitmap(FileUploadImage.FileContent);

                // Set the maximum width and height here.
                // You can make this versatile by getting these values from
                // QueryString or textboxes
                int maxWidth = 145;
                int maxHeight = 110;

                // Resize the image
                Bitmap resizedImage = CommonBLL.GetScaledPicture(uploadedImage, maxWidth, maxHeight);

                //Save the image
                // String virtualPath = "~/temp/" + System.Guid.NewGuid().ToString() + "." + ext;
                //String tempFileName = Server.MapPath(virtualPath);
                resizedImage.Save(filepathsmallimage, uploadedImage.RawFormat);
                new CmsBLL().Category_UpDateItem(int.Parse(hdItemID.Value), 0, txtTitle.Text.Trim(), strFileName, txtUrl.Text.Trim(), int.Parse("0" + rdActive.SelectedValue));
                DataTable dt = new CmsBLL().Category_ViewItemByCateID(int.Parse("0" + hdItemID.Value));
                if (dt.Rows.Count > 0)
                    LoadImage(dt.Rows[0]["Image"].ToString());
                lblMessage.Text = "Cập nhật thành công!";
                //FileUploadNews.SaveAs(filepathlargeimage);               
                // Load the resized image on the browser
                //picture.ImageUrl = virtualPath;
                //imageContainer.Visible = true;
            }
            catch (Exception ex)
            {
                lblMessage.Text = "ERROR! " + ex.Message.ToString();
            }

        }
        else
        {
            try
            {
                new CmsBLL().Category_UpDateItem(int.Parse(hdItemID.Value), 0, txtTitle.Text.Trim(), "", txtUrl.Text.Trim(), int.Parse("0" + rdActive.SelectedValue));
                lblMessage.Text = "Cập nhật thành công!";

            }
            catch (Exception ex)
            {
                lblMessage.Text = "ERROR! " + ex.Message.ToString();
            }
        }  
    }
}